<template>
    <div class="big-screen">
        <!-- 全局加载组件 -->
        <dv-loading v-if="isLoading" class="global-loading">Loading...</dv-loading>
        <!-- 顶部区域 -->
        <header class="header">
            <div class="header-left">
                <h1 class="title">陌绿陪伴数据可视化平台</h1>
                <dv-decoration-6 class="dv-dec-6" :reverse="true" :color="['#50e3c2', '#67a1e5']" />
            </div>
            <div class="header-right">
                <div class="time">{{ currentTime }}</div>
                <div class="kpi-group">
                    <div class="kpi-item">
                        <span class="label">总会员数</span>
                        <span class="value">{{ totalMembers }}</span>
                    </div>
                    <div class="kpi-item">
                        <span class="label">今日新增</span>
                        <span class="value growth">{{ todayNewMembers }}</span>
                    </div>
                    <div class="kpi-item">
                        <span class="label">总消费额(元)</span>
                        <span class="value income">{{ totalConsumption }}</span>
                    </div>
                </div>
            </div>
        </header>
        <!-- 主内容区 -->
        <main class="content">
            <!-- 左侧 -->
            <div class="left_card flex-3">
                <!-- 今日入驻滚动列表 -->
                <dv-border-box-8 class="flex-1 card_box item_crad_box">
                    <div class="scroll_box">
                        <div class="card-header">
                            <h3 class="card-title">今日入驻</h3>
                            <span class="sub-title">{{ todayCheckinCount }}人</span>
                        </div>
                        <div class="scroll-list" ref="leftScrollListRef">
                            <div class="scroll-item" v-for="(item, index) in checkinList" :key="index">
                                <div class="name">{{ item.name }}</div>
                                <div class="phone">{{ item.phone }}</div>
                                <div class="gender-age">
                                    <div class="gender">{{ item.gender }}</div>
                                    <div class="age">{{ item.age }}岁</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </dv-border-box-8>
                <dv-border-box-10 class="card_10 flex-2" style="overflow: hidden;">
                    <div style="padding: 10px;width: 100%;height: 100%;">
                        <orderBarLineChart />
                    </div>
                </dv-border-box-10>

                <dv-border-box-11 class="flex-2 card_11" title="用户增长趋势">
                    <lineChart />
                </dv-border-box-11>
            </div>
            <!-- 中间：入驻城市地图 -->
            <dv-border-box-12 class="flex-4 center-card">
                <div class="map_box">
                    <cityMap />
                </div>
            </dv-border-box-12>
            <!-- 右侧 -->
            <div class="right_card flex-3">
                <dv-border-box-8 class="card_box item_crad_box flex-1" reverse>
                    <div class="scroll_box">
                        <div class="card-header">
                            <h3 class="card-title">今日下单</h3>
                            <span class="sub-title">{{ todayOrderCount }}单</span>
                        </div>
                        <div class="scroll-list" ref="rightScrollListRef">
                            <div class="scroll-item" v-for="(item, index) in orderList" :key="index">
                                <div class="name">{{ item.name }}</div>
                                <div class="phone">{{ item.phone }}</div>
                                <div class="price—time">
                                    <div class="price">{{ item.price }}</div>
                                    <div class="time">{{ item.time }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </dv-border-box-8>
                <dv-border-box-10 class="flex-2 card_10">
                    <barLineChart />
                </dv-border-box-10>
                <div class="card_box item_crad_box flex-2 flex flex-row" style="padding: 0;gap: 0;">
                    <!-- 性别分布 -->
                    <dv-border-box-13 class="flex-1" style="padding: 20px 15px 15px;">
                        <div class="chart-container" ref="genderRef"> </div>
                    </dv-border-box-13>
                    <!-- 分润分布 -->
                    <dv-border-box-13 class="flex-2" style="padding: 20px 10px 15px;">
                        <radarChart />
                    </dv-border-box-13>
                </div>
            </div>
        </main>
        <!-- 底部区域 -->
        <footer class="footer">
            <div>系统状态：<span class="status normal">正常</span></div>
            <div>数据更新时间：{{ updateTime }}</div>
            <div>技术支持：陌绿陪伴</div>
        </footer>
    </div>
</template>

<script>
import { generateName, generatePhone, formatNumber } from "@/utils";
import * as echarts from 'echarts';
import cityMap from "@/components/cityMap";
import lineChart from "@/components/lineChart";
import barLineChart from "@/components/barLineChart";
import orderBarLineChart from "@/components/orderBarLineChart";
import radarChart from "@/components/radarChart";


// 统一颜色配置
const COLORS = {
    primary: '#4fc3f7',
    secondary: '#00e676',
    accent: '#7986cb',
    warning: '#ffb74d',
    danger: '#ef5350',
    dark: '#26a69a',
    light: '#b2ebf2',
    gradient: {
        blue: ['#4fc3f7', '#2979ff'],
        green: ['#00e676', '#00c853'],
        blueGreen: ['#4fc3f7', '#00e676']
    }
};

export default {
    components: {
        cityMap,
        lineChart,
        barLineChart,
        orderBarLineChart,
        radarChart
    },
    data() {
        return {
            totalMembers: '128,542',
            todayNewMembers: '+356',
            totalConsumption: '3,854,291',
            currentTime: '',
            updateTime: '2024-05-20 15:30:22',
            todayCheckinCount: 1850,
            todayOrderCount: 999,
            checkinList: [],
            orderList: [],
            genderData: [
                { name: '男', value: 40 },
                { name: '女', value: 55 },
                { name: '未知', value: 5 },
            ],
            profitData: [
                { name: '分润1', value: 100 },
                { name: '分润2', value: 150 },
                { name: '分润3', value: 200 },
                { name: '分润4', value: 250 },
                { name: '分润5', value: 300 },
            ],
            charts: {},
            scrollTimers: {},
            dataUpdateTimer: null,
            timeInterval: null,
            isLoading: true,
        };
    },
    mounted() {
        // 初始化当前时间并启动定时器
        this.updateCurrentTime();
        this.timeInterval = setInterval(() => this.updateCurrentTime(), 1000);

        // 初始化动态列表数据
        this.initDynamicLists();
        this.initCharts();

        // 启动数据定时更新（每5秒）
        this.startDataUpdates();
        // 监听窗口大小
        window.addEventListener('resize', this.handleResize);
    },
    beforeDestroy() {
        // 清理定时器
        if (this.timeInterval) clearInterval(this.timeInterval);
        if (this.dataUpdateTimer) clearInterval(this.dataUpdateTimer);
        // 清理滚动定时器
        Object.values(this.scrollTimers).forEach(timer => timer && clearInterval(timer));
        // 移除事件监听
        window.removeEventListener('resize', this.handleResize);
        // 销毁图表
        Object.values(this.charts).forEach(chart => chart && chart.dispose());
    },
    methods: {
        // 更新当前时间
        updateCurrentTime() {
            const date = new Date();
            this.currentTime = date.toLocaleString('zh-CN', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                hour12: false,
            });
        },
        // 初始化动态列表数据
        initDynamicLists() {
            this.checkinList = Array.from({ length: 20 }, () => ({
                name: generateName(),
                phone: generatePhone().replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3'),
                gender: Math.random() > 0.5 ? '男' : '女',
                age: Math.floor(Math.random() * 30) + 18,
            }));
            this.orderList = Array.from({ length: 20 }, () => ({
                name: generateName(),
                phone: generatePhone().replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3'),
                price: `￥${(Math.random() * 900 + 100).toFixed(2)}`,
                time: this.generateRandomTime(),
            }));
        },

        // 生成随机时间（今天）
        generateRandomTime() {
            const hours = Math.floor(Math.random() * 12) + 9;
            const minutes = Math.floor(Math.random() * 60);
            const seconds = Math.floor(Math.random() * 60);
            return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
        },
        // 初始化图表
        initCharts() {
            // 性别分布图表
            if (this.$refs.genderRef) {
                this.charts.gender = echarts.init(this.$refs.genderRef);
                this.updateGenderChart();
            }
            // 分润分布图表
            if (this.$refs.profitRef) {
                this.charts.profit = echarts.init(this.$refs.profitRef);
                this.updateProfitChart();
            }
            setTimeout(() => {
                this.isLoading = false
                this.initAutoScroll(1, ['leftScrollListRef', 'rightScrollListRef']);
            }, 1000)
        },
        // 更新性别分布图表
        updateGenderChart() {
            if (!this.charts.gender || !this.genderData) return;
            const colorList = [COLORS.primary, COLORS.secondary, COLORS.accent];
            this.charts.gender.setOption({
                tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' },
                backgroundColor: '#0C1121',
                title: {
                    text: '性别分布',
                    textStyle: {
                        color: '#02C2FF',
                        fontSize: 16
                    },
                    left: 10,
                    top: 10
                },
                legend: {
                    textStyle: { color: '#e3f2fd' },
                    orient: 'horizontal',
                    itemWidth: 12,
                    itemHeight: 12,
                    itemGap: 15,
                    bottom: 10
                },
                series: [{
                    name: '性别分布',
                    type: 'pie',
                    radius: ['40%', '70%'],
                    center: ['50%', '45%'],
                    avoidLabelOverlap: false,
                    itemStyle: {
                        borderRadius: 6,
                        borderColor: 'rgba(30, 42, 86, 0.8)',
                        borderWidth: 2
                    },
                    label: { show: false, position: 'center' },
                    emphasis: {
                        label: { show: true, fontSize: 16, fontWeight: 'bold', color: '#fff' }
                    },
                    labelLine: { show: false },
                    data: this.genderData.map((item, index) => ({
                        name: item.name,
                        value: item.value,
                        itemStyle: { color: colorList[index % colorList.length] }
                    }))
                }]
            });
        },
        // 更新分润分布图表
        updateProfitChart() {
            if (!this.charts.profit || !this.profitData) return;
            const colorList = [COLORS.primary, COLORS.secondary, COLORS.accent, COLORS.warning, COLORS.dark];
            this.charts.profit.setOption({
                title: {
                    text: '分润分布',
                    textStyle: {
                        color: '#02C2FF',
                        fontSize: 16,
                    },
                    left: 10,
                    top: 10
                },
                backgroundColor: '#0C1121',
                tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' },
                legend: {
                    bottom: 0,
                    textStyle: { color: '#e3f2fd' },
                    orient: 'horizontal',
                    itemWidth: 12,
                    itemHeight: 12,
                    itemGap: 15
                },
                series: [{
                    name: '分润分布',
                    type: 'pie',
                    radius: ['40%', '70%'],
                    center: ['50%', '50%'],
                    avoidLabelOverlap: false,
                    itemStyle: {
                        borderRadius: 6,
                        borderColor: 'rgba(30, 42, 86, 0.8)',
                        borderWidth: 2
                    },
                    emphasis: {
                        label: { show: true, fontSize: 16, fontWeight: 'bold', color: '#fff' }
                    },
                    label: {
                        alignTo: 'edge',
                        formatter: '{name|{b}}\n{time|{c} 元}',
                        edgeDistance: 15,
                        margin: 40,
                        lineHeight: 15,
                        rich: { time: { fontSize: 10, color: '#999' } }
                    },
                    labelLine: { length: 10, length2: 40, maxSurfaceAngle: 45 },
                    data: this.profitData.map((item, index) => ({
                        name: item.name,
                        value: item.value,
                        itemStyle: { color: colorList[index % colorList.length] }
                    }))
                }]
            });
        },
        // 初始化自动滚动
        initAutoScroll(speed, refNames) {
            refNames.forEach(refName => {
                const container = this.$refs[refName];
                if (!container) return;
                if (this.scrollTimers[refName]) clearInterval(this.scrollTimers[refName]);
                if (this.scrollTimers[refName]) clearInterval(this.scrollTimers[refName]);

                let scrollTop = 0;
                const scrollHeight = container.scrollHeight;
                const clientHeight = container.clientHeight;

                this.scrollTimers[refName] = setInterval(() => {
                    scrollTop += speed;
                    if (scrollTop >= scrollHeight - clientHeight) scrollTop = 0;
                    if (scrollTop >= scrollHeight - clientHeight) scrollTop = 0;
                    container.scrollTop = scrollTop;
                }, 50);

                // 鼠标悬停暂停
                container.addEventListener('mouseenter', () => {
                    if (this.scrollTimers[refName]) {
                        clearInterval(this.scrollTimers[refName]);
                        this.scrollTimers[refName] = null;
                    }
                });
                // 鼠标离开继续
                container.addEventListener('mouseleave', () => {
                    this.initAutoScroll(speed, [refName]);
                    this.initAutoScroll(speed, [refName]);
                });
            });
        },
        // 启动数据定时更新
        startDataUpdates() {
            if (this.dataUpdateTimer) {
                clearInterval(this.dataUpdateTimer)
            }
            this.dataUpdateTimer = setInterval(() => {
                // 更新会员数据
                const totalMembersNum = parseInt(this.totalMembers.replace(/,/g, '')) + Math.floor(Math.random() * 10);
                this.totalMembers = formatNumber(totalMembersNum);

                const todayNewNum = parseInt(this.todayNewMembers.replace(/\+|,/g, '')) + Math.floor(Math.random() * 3);
                this.todayNewMembers = `+${formatNumber(todayNewNum)}`;

                const totalConsumptionNum = parseInt(this.totalConsumption.replace(/,/g, '')) + Math.floor(Math.random() * 1000);
                this.totalConsumption = formatNumber(totalConsumptionNum);

                // 更新数据时间
                this.updateTime = new Date().toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit',
                    hour12: false,
                });
            }, 5000);
        },

        // 处理窗口大小变化
        handleResize() {
            Object.values(this.charts).forEach(chart => chart && chart.resize());
        }
    }
};
</script>

<style scoped lang="scss">
/* 全局样式重置 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;

    ::-webkit-scrollbar {
        display: none;
    }
}

// 公共工具类
.flex {
    display: flex;
}

.flex-row {
    flex-direction: row !important;
}

.flex-col {
    flex-direction: column;
}

.flex-1 {
    flex: 1;
}

.flex-2 {
    flex: 2;
}

.flex-3 {
    flex: 3;
}

.flex-4 {
    flex: 4;
}

.h-100 {
    height: inherit !important;
}

// 全局加载组件样式
.global-loading {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: #00ff9d;
    z-index: 9999;
}

.big-screen {
    width: 100vw;
    height: 100vh;
    background-image: url("../assets/pageBg.png");
    background-size: cover;
    background-position: center center;
    color: #fff;
    overflow: hidden;
    position: relative;
}

/* 顶部样式 */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80px;
    padding: 0 30px;
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    position: relative;
    z-index: 2;

    .header-left {
        position: relative;

        .title {
            font-size: 28px;
            font-weight: bold;
            background: linear-gradient(90deg, #00c2ff, #00ff9d);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            text-shadow: 0 0 12px rgba(0, 255, 157, 0.2);
            line-height: 40px;
        }

        .dv-dec-6 {
            position: absolute;
            bottom: -10px;
            left: 50%;
            width: 250px;
            height: 10px;
            transform: translate(-50%);
        }
    }


    .header-right {
        display: flex;
        align-items: center;
        gap: 40px;

        .time {
            font-size: 16px;
            color: #81d4fa;
            opacity: 0.9;
        }
    }
}

/* KPI指标组 */
.kpi-group {
    display: flex;
    gap: 30px;

    .kpi-item {
        text-align: center;

        .label {
            display: block;
            font-size: 12px;
            color: #b0bec5;
            margin-bottom: 4px;
        }

        .value {
            font-size: 18px;
            font-weight: 600;

            &.growth {
                color: #00ff9d;
                text-shadow: 0 0 8px rgba(0, 255, 157, 0.3);
            }

            &.income {
                color: #ff7d00;
                text-shadow: 0 0 8px rgba(255, 125, 0, 0.3);
            }
        }
    }
}

/* 主内容区 */
.content {
    display: flex;
    height: calc(100vh - 130px);
    padding: 15px 0;
    position: relative;
    z-index: 1;
    margin: 0 auto;
    width: 98%;

    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;

        h3 {
            font-size: 18px;
            color: #00c2ff;
            display: flex;
            align-items: center;

            &::before {
                content: '';
                display: inline-block;
                width: 6px;
                height: 20px;
                background: #00c2ff;
                margin-right: 8px;
                border-radius: 3px;
            }
        }

        .sub-title {
            font-size: 18px;
            font-weight: 600;
            color: #00c2ff;
            letter-spacing: 1px;
        }
    }

    .card_box {
        border-radius: 12px;
        padding: 20px;
        background-color: #0C1121;
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 10px;

        &>.chart-container {
            flex: 1;
            min-height: 150px;
        }
    }

    .card_11 {
        padding: 60px 20px 20px;
    }

    .center-card {
        margin: 0 10px;
        padding: 15px;
        margin: 0 10px;
        padding: 15px;

        .map_box {
            width: 100%;
            height: 100%;
            background-color: #0C1121;
            border-radius: 12px;
        }
    }

    .left_card,
    .right_card {
        display: flex;
        flex-direction: column;
        gap: 10px;

        .item_crad_box {
            background-color: transparent;
            overflow: hidden;
            padding: 10px;
            border-radius: 0;

            .scroll_box {
                height: inherit;
                display: flex;
                flex-direction: column;
                gap: 5px;
                background-color: #0C1121;
                padding: 10px;
                border-radius: 10px;

                .scroll-list {
                    flex: 1;
                    overflow: scroll;

                    .scroll-item {
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        padding: 8px 0;
                        border-bottom: 1px solid rgba(255, 255, 255, 0.1);

                        .name {
                            font-size: 16px;
                            font-weight: 600;
                            width: 20%;
                            color: #00c2ff;
                        }

                        .phone {
                            font-size: 14px;
                            color: #b0bec5;
                            letter-spacing: 1px;
                            width: 20%;
                        }

                        .gender-age,
                        .price—time {
                            font-size: 14px;
                            color: #b0bec5;
                            display: flex;
                            gap: 10px;
                            align-items: center;
                            width: 30%;
                            justify-content: flex-end;

                            .price {
                                color: #00ff9d;
                                font-weight: 500;
                            }
                        }
                    }
                }
            }
        }

        .chart-container {
            width: 100%;
            height: 100%;
            min-height: 150px;
            height: 100%;
            min-height: 150px;
        }
    }
}

/* 底部区域 */
.footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    padding: 0 30px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    color: #fff;
    font-size: 14px;
    position: relative;
    z-index: 2;

    .status {
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 12px;
        margin: 0 4px;
    }

    .normal {
        background: #00ff9d;
        color: #0f1733;
    }
}
</style>